<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Batch 核心源码深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-cogs text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Spring Batch 核心源码深度解析</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索企业级批处理框架的内部机制，从 Job 到 Step，从 ItemReader 到 ItemWriter，全面剖析 Spring Batch 的设计精髓
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                Spring Batch 是一个用于批量处理的框架，它提供了非常强大和灵活的功能，如事务管理、作业执行、处理数据的分段等。我们将逐步分析其核心组件和工作流程，具体包括 Job、Step 的执行流程、ItemReader、ItemProcessor、ItemWriter 等的实现。
            </p>
        </div>

        <!-- Architecture Overview -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 gradient-text">架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[JobLauncher] --> B[Job]
                    B --> C[Step 1]
                    B --> D[Step 2]
                    B --> E[Step N]
                    C --> F[ItemReader]
                    C --> G[ItemProcessor]
                    C --> H[ItemWriter]
                    I[JobRepository] --> A
                    J[JobExecutionListener] --> B
                    K[StepExecutionListener] --> C
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1: Core Structure -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8">
                <i class="fas fa-layer-group mr-3 text-purple-600"></i>
                1. Spring Batch 核心结构
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                Spring Batch 的核心是基于 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Job</code> 和 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 构建的。一个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Job</code> 由多个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 组成，每个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 都代表了批处理中的一个阶段。每个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 内部包括了数据的读取、处理和写入。Spring Batch 通过这些组件完成批量任务的处理。
            </p>

            <div class="highlight-box">
                <h3 class="text-xl font-bold mb-3">
                    <i class="fas fa-code mr-2"></i>
                    Job 和 Step 的定义
                </h3>
            </div>

            <div class="code-block mt-6">
                <pre><code>public interface Job {
    String getName();
    JobExecution run(JobParameters jobParameters) throws JobExecutionException;
}

public interface Step {
    String getName();
    ExitStatus execute(StepExecution stepExecution) throws StepExecutionException;
}</code></pre>
            </div>

            <div class="mt-8 space-y-4">
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <p class="text-gray-700"><code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Job</code> 是整个批处理作业的入口，它可以包含多个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code>，每个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 负责完成某一部分工作。</p>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                    <p class="text-gray-700"><code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 是 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Job</code> 中的具体任务，负责数据的读取、处理和写入。</p>
                </div>
            </div>
        </div>

        <!-- Section 2: JobLauncher -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8">
                <i class="fas fa-rocket mr-3 text-purple-600"></i>
                2. JobLauncher 的工作流程
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">JobLauncher</code> 是启动批处理作业的入口，它负责启动一个作业并处理作业的执行状态。在执行作业时，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">JobLauncher</code> 会创建 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">JobExecution</code> 对象，并通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Job</code> 来启动作业。
            </p>

            <div class="code-block">
                <pre><code>public interface JobLauncher {
    JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionException;
}

public class SimpleJobLauncher implements JobLauncher {
    private JobRepository jobRepository;

    @Override
    public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionException {
        // 1. 创建 JobExecution
        JobExecution jobExecution = createJobExecution(job, jobParameters);

        // 2. 执行作业
        job.execute(jobExecution);

        // 3. 保存作业执行状态
        jobRepository.update(jobExecution);
        return jobExecution;
    }
}</code></pre>
            </div>

            <div class="mt-8 grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-purple-600 text-3xl mb-3">
                        <i class="fas fa-play-circle"></i>
                    </div>
                    <h4 class="font-bold mb-2">创建执行实例</h4>
                    <p class="text-sm text-gray-600">创建 JobExecution 实例，包含作业执行的状态信息</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-purple-600 text-3xl mb-3">
                        <i class="fas fa-cog"></i>
                    </div>
                    <h4 class="font-bold mb-2">执行作业</h4>
                    <p class="text-sm text-gray-600">通过 job.execute() 启动作业执行</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-purple-600 text-3xl mb-3">
                        <i class="fas fa-save"></i>
                    </div>
                    <h4 class="font-bold mb-2">保存状态</h4>
                    <p class="text-sm text-gray-600">jobRepository.update() 保存作业执行状态</p>
                </div>
            </div>
        </div>

        <!-- Section 3: StepExecution -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8">
                <i class="fas fa-database mr-3 text-purple-600"></i>
                3. StepExecution 和事务管理
            </h2>
            
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                每个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 在执行时，都会创建一个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">StepExecution</code> 实例，它包含了该步骤的执行状态、步骤的开始和结束时间、处理的项目数等信息。Spring Batch 使用事务管理器来确保每个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Step</code> 的数据处理是原子的。
            </p>

            <div class="code-block">
                <pre><code>public class StepExecution {
    